package com.ets.common;

import java.text.NumberFormat;

import com.ets.Address_DAO.AddressDAO;
import com.ets.Address_DAO.AddressDB;
import com.ets.Codes_DAO.CodeValueDAO;
import com.ets.Codes_DAO.CodeValueDB;
import com.ets.Vendor_DAO.VendorDAO;
import com.ets.Vendor_DAO.VendorDB;

/*------------------------------------------------------------------------------
 System-wide <big><b>Validator</b></big> class, which provides overall unit
 validation
 <br><br>
 @author D.Erekson - 1:33:38 PM - Aug 22, 2011
 <br><br>
 @version 1.0  
------------------------------------------------------------------------------*/
public abstract class Validator
  {
   // Declare class variables
   private static AddressDAO   addressDAO   = null;
   private static CodeValueDAO codevalueDAO = null;
   private static VendorDAO    vendorDAO    = null;

   public static String numberFormatter (double d)
	 {
	  NumberFormat nf = NumberFormat.getCurrencyInstance();
	  nf.setMaximumFractionDigits(2);
	  nf.setMinimumFractionDigits(2);
	  return nf.format(d);
	 }

   public static String validatePhone (String Phn)
     {
  	  String inPhn  = Phn.trim();
      String outPhn = "";
	  if (inPhn.length() < 10)
	    {
    	 System.out.println("Phone string less than 10 digits");
		 return "**BAD**";
	    }
     outPhn = Formatter.formatPhone(inPhn);
     if (outPhn.equals("**BAD**"))
       {
 	    System.out.println("Phone string less than 10 digits");
        return outPhn;
       }
	 return outPhn;
   }
   
   public static boolean validateEmail (String Eml)
     {
	  String inEml  = Eml.trim();
	  if (inEml.length() > 60)
        {
	     System.out.println("Email string more than 60 characters");
	     return false;
	    }
	  if (inEml.indexOf('@') == -1)
	    {
	     System.out.println("Email string does not contain '@'");
	     return false;
	    }
	  if (inEml.indexOf(".net") == -1 &&
		  inEml.indexOf(".com") == -1 &&
          inEml.indexOf(".gov") == -1 &&
		  inEml.indexOf(".biz") == -1   )
	    {
	     System.out.println("Email string does not contain valid domain type");
	     return false;
	    }
	  return true;
     }
   
   public static String validateZip (String Zp)
     {
      String inZp  = Zp.trim();
      String outZp = "";
      if (inZp.length() < 5)
        {
	     System.out.println("Zip string less than 5 digits");
	     return "**BAD**";
        }
      outZp = Formatter.formatZip(inZp);
      if (outZp.equals("**BAD**"))
        {
	     System.out.println("Zip string invalid");
 	     return outZp;
        }
      return outZp;
     }
   
   public static boolean isVendorValid (String Vndr)
     {
      String    tmpVndr   = Vndr.trim();
                tmpVndr   = tmpVndr.toUpperCase();
       	        vendorDAO = DAOFactory.getVendorDAO();
   	  VendorDAO vDAO      = new VendorDB();
      boolean   isValid   = vDAO.existsVendor(tmpVndr);
      return isValid;
     }
   
   public static boolean isCodeValid (String Sys, String Sgmt, String Cd)
     {
	  String       tmpSys       = Sys.trim();
                   tmpSys       = tmpSys.toUpperCase();
      String       tmpSgmt      = Sgmt.trim();
                   tmpSgmt      = tmpSgmt.toUpperCase();
      String       tmpCd        = Cd.trim();
                   tmpCd        = tmpCd.toUpperCase();
          	       codevalueDAO = DAOFactory.getCodeValueDAO();
      CodeValueDAO cvDAO        = new CodeValueDB();
      boolean      isValid      = cvDAO.existsCode(tmpSys, tmpSgmt, tmpCd);
      return isValid;
     }
   
   public static boolean isAddressValid (int Addr)
     {
      addressDAO         = DAOFactory.getAddressDAO();
   	  AddressDAO aDAO    = new AddressDB();
      boolean    isValid = aDAO.existsAddress(Addr);
      return isValid;
     }

	/*----------------------------------------------------------------------------*/
	/* Date:    2012-02-21                                                        */
	/* Author:  David Erekson                                                     */
	/* Purpose: MVA overall unit validation                                       */
	/*----------------------------------------------------------------------------*/
	
	// new code goes here
	
  }
